﻿/*
 *  Source code courtesy of the desktopWeb.CodePlex.com community project. See MS-PL license on Codeplex.com. 
 *  This is beta code not intended for a production application.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities.Presentation.Model;
using System.Activities;
using Microsoft.VisualBasic.Activities;
using System.Activities.Statements;

namespace MyData.ExpressionBuilder.Model
{
  public static class ExpressionExtensions
  {
    public static object TargetExpression(this ModelItem expression, string path)
    {
      //Change
      //  expression == null ? string.Empty : expression.ToString();
      // ExpressionText or Value
      //VisualBasicValue<Object>
      object expressionText = null;

      //NEXT: Needs to be more abstract
      if (expression != null)
      {
        if (expression.Source.ComputedValue.GetType() == typeof(VisualBasicValue<Object>))
        {
          expressionText = (expression.Source.ComputedValue as VisualBasicValue<Object>).ExpressionText.NonQuotedValue();
        }
        else if (expression.Source.ComputedValue.GetType() == typeof(VisualBasicValue<Dictionary<String, Object>>))
        {
          expressionText = (expression.Source.ComputedValue as VisualBasicValue<Dictionary<String, Object>>).ExpressionText;
        }
        else
        {
          expressionText  = expression.Source.ComputedValue.ToString();
        }
      }
      return expressionText;
    }

    public static object Expression<T>(this ModelItem modelItem, string path)
    {
      InArgument<T> computedValue = null;
      computedValue = modelItem.Properties[path].ComputedValue as InArgument<T>;

      return computedValue.Expression;
    }


    //TODO: Fix: Should not be .Parent.Parent, SB typeof(Sequence)
    public static ModelItem SequenceActivity(this ModelItem modelItem, string path)
    {
      ModelItem mi = null;
      if (modelItem.Parent.Parent != null)
      {
        mi = modelItem.Parent.Parent;        
      }
      return mi;
    }


    //Recursive loop .Parent until parentPath
    public static ModelItem BaseActivity(this ModelItem activity, string baseTypeName)
    {
      ModelItem mi = null;
      if (activity != null)
        mi = GetParent(activity, baseTypeName);
      return mi;
    }


    //TODO: SB typeof(Type)
    private static ModelItem GetParent(ModelItem mi, string name)
    {      
      while (mi != null)
      {
        if (mi.ItemType.BaseType.Name == name)
          break;
        mi = GetParent(mi.Parent, name);
      }

      return mi;
    }

    public static ModelItem GetParent(this ModelItem mi, Type type)
    {      
      while (mi != null)
      {
        if (mi.ItemType.Name == type.Name)
          break;
        mi = GetParent(mi.Parent, type);
      }

      return mi;
    }


  }

  //TODO: Remove - Duplicate code
  public static class StringExtensions
  {
    public static string QuotedValue(this String value)
    {
      value = value != null ? value : string.Empty;
      return "\"" + value + "\"";
    }

  public static string NonQuotedValue(this String value)
    {
      value = value != null ? value : string.Empty;
      return value.Replace("\"", string.Empty);
    }
  }

}
